package cn.zou.core.shiro.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authc.credential.Md5CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class MyRealm extends AuthorizingRealm {
	 public static int HashIterations=1024;
	 public static String source="abcd";
	 public static ByteSource credentialsSalt=null;
	  static{
		  credentialsSalt=new Md5Hash(source);
	 }
	/**
	 * 授权
	 */
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalConllection) {
		// TODO 自动生成的方法存根
		
		return null;
	}

	/**
	 * 认证
	 */
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// TODO 自动生成的方法存根
		//根据username获取到一个实体对象
		Object principal= token.getPrincipal();
		
		String hashedCredentials="68f3139a38b232392cc9d3b6ddd762f7";
		
		//e2fc714c4727ee9395f324cd2e7f331f
		//reaml的name
		String realmName=getName();
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(principal, hashedCredentials, credentialsSalt, realmName);
		return info;
	}
	/**
	 * 应该在启动时就加载好加密方式
	 */
	public void setCredentialMatcher(){
		HashedCredentialsMatcher credentialMatcher=new HashedCredentialsMatcher();
		//设置加密方式
		credentialMatcher.setHashAlgorithmName("MD5");
		//设置加密的次数
		credentialMatcher.setHashIterations(HashIterations);
		setCredentialsMatcher(credentialMatcher);
	}
}
